// security.js

const biRadixBase = 2;
const biRadixBits = 16;
const bitsPerDigit = biRadixBits;
const biRadix = 1 << 16; // = 2^16 = 65536
const biHalfRadix = biRadix >>> 1;
const biRadixSquared = biRadix * biRadix;
const maxDigitVal = biRadix - 1;
const maxInteger = 9999999999999998;

let maxDigits;
let ZERO_ARRAY;
let bigZero, bigOne;

class BigInt {
    constructor(flag) {
        if (typeof flag === "boolean" && flag === true) {
            this.digits = null;
        } else {
            this.digits = ZERO_ARRAY.slice(0);
        }
        this.isNeg = false;
    }
}

const setMaxDigits = (value) => {
    maxDigits = value;
    ZERO_ARRAY = new Array(maxDigits);
    for (let iza = 0; iza < ZERO_ARRAY.length; iza++) {
        ZERO_ARRAY[iza] = 0;
    }
    bigZero = new BigInt();
    bigOne = new BigInt();
    bigOne.digits[0] = 1;
};

setMaxDigits(20);

const dpl10 = 15;

const biFromNumber = (i) => {
    const result = new BigInt();
    result.isNeg = i < 0;
    i = Math.abs(i);
    let j = 0;
    while (i > 0) {
        result.digits[j++] = i & maxDigitVal;
        i = Math.floor(i / biRadix);
    }
    return result;
};

const lr10 = biFromNumber(1000000000000000);

const biFromDecimal = (s) => {
    const isNeg = s.charAt(0) === '-';
    let i = isNeg ? 1 : 0;
    let result;
    while (i < s.length && s.charAt(i) === '0') ++i;
    if (i === s.length) {
        result = new BigInt();
    } else {
        const digitCount = s.length - i;
        let fgl = digitCount % dpl10;
        if (fgl === 0) fgl = dpl10;
        result = biFromNumber(Number(s.substr(i, fgl)));
        i += fgl;
        while (i < s.length) {
            result = biAdd(biMultiply(result, lr10), biFromNumber(Number(s.substr(i, dpl10))));
            i += dpl10;
        }
        result.isNeg = isNeg;
    }
    return result;
};
const biMultiply = (x, y)=> {
    let result = new BigInt();
    let c;
    let n = biHighIndex(x);
    let t = biHighIndex(y);
    let u, uv, k;

    for (let i = 0; i <= t; ++i) {
        c = 0;
        k = i;
        for (let j = 0; j <= n; ++j, ++k) {
            uv = result.digits[k] + x.digits[j] * y.digits[i] + c;
            result.digits[k] = uv & maxDigitVal;
            c = uv >>> biRadixBits;
            //c = Math.floor(uv / biRadix);
        }
        result.digits[i + n + 1] = c;
    }
    // Someone give me a logical xor, please.
    result.isNeg = x.isNeg != y.isNeg;
    return result;
};
const biMultiplyDigit = (x, y) =>{
    let n, c, uv;

   let result = new BigInt();
    n = biHighIndex(x);
    c = 0;
    for (let j = 0; j <= n; ++j) {
        uv = result.digits[j] + x.digits[j] * y + c;
        result.digits[j] = uv & maxDigitVal;
        c = uv >>> biRadixBits;
        //c = Math.floor(uv / biRadix);
    }
    result.digits[1 + n] = c;
    return result;
};

const biCopy = (bi) => {
    const result = new BigInt(true);
    result.digits = bi.digits.slice(0);
    result.isNeg = bi.isNeg;
    return result;
};

const reverseStr = (s) => {
    let result = "";
    for (let i = s.length - 1; i > -1; --i) {
        result += s.charAt(i);
    }
    return result;
};

const hexatrigesimalToChar = [
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
    'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
    'u', 'v', 'w', 'x', 'y', 'z'
];

const biToString = (x, radix) => {
    const b = new BigInt();
    b.digits[0] = radix;
    let qr = biDivideModulo(x, b);
    let result = hexatrigesimalToChar[qr[1].digits[0]];
    while (biCompare(qr[0], bigZero) === 1) {
        qr = biDivideModulo(qr[0], b);
        result += hexatrigesimalToChar[qr[1].digits[0]];
    }
    return (x.isNeg ? "-" : "") + reverseStr(result);
};

const biToDecimal = (x) => {
    const b = new BigInt();
    b.digits[0] = 10;
    let qr = biDivideModulo(x, b);
    let result = String(qr[1].digits[0]);
    while (biCompare(qr[0], bigZero) === 1) {
        qr = biDivideModulo(qr[0], b);
        result += String(qr[1].digits[0]);
    }
    return (x.isNeg ? "-" : "") + reverseStr(result);
};

const hexToChar = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];

const digitToHex = (n) => {
    let mask = 0xf;
    let result = "";
    for (let i = 0; i < 4; ++i) {
        result += hexToChar[n & mask];
        n >>>= 4;
    }
    return reverseStr(result);
};

const biToHex = (x) => {
    let result = "";
    const n = biHighIndex(x);
    for (let i = n; i > -1; --i) {
        result += digitToHex(x.digits[i]);
    }
    return result;
};

const charToHex = (c) => {
    const ZERO = 48;
    const NINE = ZERO + 9;
    const littleA = 97;
    const littleZ = littleA + 25;
    const bigA = 65;
    const bigZ = bigA + 25;
    let result;

    if (c >= ZERO && c <= NINE) {
        result = c - ZERO;
    } else if (c >= bigA && c <= bigZ) {
        result = 10 + c - bigA;
    } else if (c >= littleA && c <= littleZ) {
        result = 10 + c - littleA;
    } else {
        result = 0;
    }
    return result;
};

const hexToDigit = (s) => {
    let result = 0;
    const sl = Math.min(s.length, 4);
    for (let i = 0; i < sl; ++i) {
        result <<= 4;
        result |= charToHex(s.charCodeAt(i));
    }
    return result;
};

const biFromHex = (s) => {
    const result = new BigInt();
    const sl = s.length;
    for (let i = sl, j = 0; i > 0; i -= 4, ++j) {
        result.digits[j] = hexToDigit(s.substr(Math.max(i - 4, 0), Math.min(i, 4)));
    }
    return result;
};

const biFromString = (s, radix) => {
    const isNeg = s.charAt(0) === '-';
    const istop = isNeg ? 1 : 0;
    const result = new BigInt();
    const place = new BigInt();
    place.digits[0] = 1;
    for (let i = s.length - 1; i >= istop; i--) {
        const c = s.charCodeAt(i);
        const digit = charToHex(c);
        const biDigit = biMultiplyDigit(place, digit);
        result.digits = biAdd(result, biDigit).digits;
        place.digits = biMultiplyDigit(place, radix).digits;
    }
    result.isNeg = isNeg;
    return result;
};

const biDump = (b) => (b.isNeg ? "-" : "") + b.digits.join(" ");

const biAdd = (x, y) => {
    const result = new BigInt();
    if (x.isNeg !== y.isNeg) {
        y.isNeg = !y.isNeg;
        const subResult = biSubtract(x, y);
        y.isNeg = !y.isNeg;
        return subResult;
    }

    let c = 0;
    for (let i = 0; i < x.digits.length; ++i) {
        const n = x.digits[i] + y.digits[i] + c;
        result.digits[i] = n % biRadix;
        c = Number(n >= biRadix);
    }
    result.isNeg = x.isNeg;
    return result;
};

const biSubtract = (x, y) => {
    const result = new BigInt();
    if (x.isNeg !== y.isNeg) {
        y.isNeg = !y.isNeg;
        const addResult = biAdd(x, y);
        y.isNeg = !y.isNeg;
        return addResult;
    }

    let c = 0;
    for (let i = 0; i < x.digits.length; ++i) {
        let n = x.digits[i] - y.digits[i] + c;
        result.digits[i] = n % biRadix;
        if (result.digits[i] < 0) result.digits[i] += biRadix;
        c = 0 - Number(n < 0);
    }

    if (c === -1) {
        c = 0;
        for (let i = 0; i < x.digits.length; ++i) {
            let n = 0 - result.digits[i] + c;
            result.digits[i] = n % biRadix;
            if (result.digits[i] < 0) result.digits[i] += biRadix;
            c = 0 - Number(n < 0);
        }
        result.isNeg = !x.isNeg;
    } else {
        result.isNeg = x.isNeg;
    }
    return result;
};

const biHighIndex = (x) => {
    let result = x.digits.length - 1;
    while (result > 0 && x.digits[result] === 0) --result;
    return result;
};

const biNumBits = (x) => {
    const n = biHighIndex(x);
    let d = x.digits[n];
    const m = (n + 1) * bitsPerDigit;
    let result;
    for (result = m; result > m - bitsPerDigit; --result) {
        if ((d & 0x8000) !== 0) break;
        d <<= 1;
    }
    return result;
};

const arrayCopy = (src, srcStart, dest, destStart, n) => {
    const m = Math.min(srcStart + n, src.length);
    for (let i = srcStart, j = destStart; i < m; ++i, ++j) {
        dest[j] = src[i];
    }
};

const highBitMasks = [0x0000, 0x8000, 0xC000, 0xE000, 0xF000, 0xF800,
    0xFC00, 0xFE00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0,
    0xFFF0, 0xFFF8, 0xFFFC, 0xFFFE, 0xFFFF];

const biShiftLeft = (x, n) => {
    const digitCount = Math.floor(n / bitsPerDigit);
    const result = new BigInt();
    arrayCopy(x.digits, 0, result.digits, digitCount, result.digits.length - digitCount);
    const bits = n % bitsPerDigit;
    const rightBits = bitsPerDigit - bits;
    for (let i = result.digits.length - 1, i1 = i - 1; i > 0; --i, --i1) {
        result.digits[i] = ((result.digits[i] << bits) & maxDigitVal) |
            ((result.digits[i1] & highBitMasks[bits]) >>> rightBits);
    }
    result.digits[0] = (result.digits[0] << bits) & maxDigitVal;
    result.isNeg = x.isNeg;
    return result;
};

const lowBitMasks = [0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F,
    0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF,
    0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF];

const biShiftRight = (x, n) => {
    const digitCount = Math.floor(n / bitsPerDigit);
    const result = new BigInt();
    arrayCopy(x.digits, digitCount, result.digits, 0, x.digits.length - digitCount);
    const bits = n % bitsPerDigit;
    const leftBits = bitsPerDigit - bits;
    for (let i = 0, i1 = i + 1; i < result.digits.length - 1; ++i, ++i1) {
        result.digits[i] = (result.digits[i] >>> bits) |
            ((result.digits[i1] & lowBitMasks[bits]) << leftBits);
    }
    result.digits[result.digits.length - 1] >>>= bits;
    result.isNeg = x.isNeg;
    return result;
};

const biMultiplyByRadixPower = (x, n) => {
    const result = new BigInt();
    arrayCopy(x.digits, 0, result.digits, n, result.digits.length - n);
    return result;
};

const biDivideByRadixPower = (x, n) => {
    const result = new BigInt();
    arrayCopy(x.digits, n, result.digits, 0, result.digits.length - n);
    return result;
};

const biModuloByRadixPower = (x, n) => {
    const result = new BigInt();
    arrayCopy(x.digits, 0, result.digits, 0, n);
    return result;
};

const biCompare = (x, y) => {
    if (x.isNeg !== y.isNeg) return 1 - 2 * Number(x.isNeg);
    for (let i = x.digits.length - 1; i >= 0; --i) {
        if (x.digits[i] !== y.digits[i]) {
            return x.isNeg
                ? 1 - 2 * Number(x.digits[i] > y.digits[i])
                : 1 - 2 * Number(x.digits[i] < y.digits[i]);
        }
    }
    return 0;
};

const biDivideModulo = (x, y) => {
    const nb = biNumBits(x);
    let tb = biNumBits(y);
    const origYIsNeg = y.isNeg;
    let q, r;

    if (nb < tb) {
        if (x.isNeg) {
            q = biCopy(bigOne);
            q.isNeg = !y.isNeg;
            x.isNeg = false;
            y.isNeg = false;
            r = biSubtract(y, x);
            x.isNeg = true;
            y.isNeg = origYIsNeg;
        } else {
            q = new BigInt();
            r = biCopy(x);
        }
        return [q, r];
    }

    q = new BigInt();
    r = x;

    let t = Math.ceil(tb / bitsPerDigit) - 1;
    let lambda = 0;
    while (y.digits[t] < biHalfRadix) {
        y = biShiftLeft(y, 1);
        ++lambda;
        ++tb;
        t = Math.ceil(tb / bitsPerDigit) - 1;
    }

    r = biShiftLeft(r, lambda);
    let n = Math.ceil((nb + lambda) / bitsPerDigit) - 1;

    let b = biMultiplyByRadixPower(y, n - t);
    while (biCompare(r, b) !== -1) {
        ++q.digits[n - t];
        r = biSubtract(r, b);
    }

    for (let i = n; i > t; --i) {
        const ri = i >= r.digits.length ? 0 : r.digits[i];
        const ri1 = (i - 1 >= r.digits.length) ? 0 : r.digits[i - 1];
        const ri2 = (i - 2 >= r.digits.length) ? 0 : r.digits[i - 2];
        const yt = t >= y.digits.length ? 0 : y.digits[t];
        const yt1 = (t - 1 >= y.digits.length) ? 0 : y.digits[t - 1];

        q.digits[i - t - 1] = ri === yt ? maxDigitVal : Math.floor((ri * biRadix + ri1) / yt);

        let c1 = q.digits[i - t - 1] * ((yt * biRadix) + yt1);
        let c2 = (ri * biRadixSquared) + ((ri1 * biRadix) + ri2);
        while (c1 > c2) {
            --q.digits[i - t - 1];
            c1 = q.digits[i - t - 1] * ((yt * biRadix) | yt1);
            c2 = (ri * biRadix * biRadix) + ((ri1 * biRadix) + ri2);
        }

        b = biMultiplyByRadixPower(y, i - t - 1);
        r = biSubtract(r, biMultiplyDigit(b, q.digits[i - t - 1]));
        if (r.isNeg) {
            r = biAdd(r, b);
            --q.digits[i - t - 1];
        }
    }

    r = biShiftRight(r, lambda);
    q.isNeg = x.isNeg !== origYIsNeg;
    if (x.isNeg) {
        if (origYIsNeg) {
            q = biAdd(q, bigOne);
        } else {
            q = biSubtract(q, bigOne);
        }
        y = biShiftRight(y, lambda);
        r = biSubtract(y, r);
    }

    if (r.digits[0] === 0 && biHighIndex(r) === 0) r.isNeg = false;

    return [q, r];
};

const biDivide = (x, y) => biDivideModulo(x, y)[0];
const biModulo = (x, y) => biDivideModulo(x, y)[1];
const biMultiplyMod = (x, y, m) => biModulo(biMultiply(x, y), m);

const biPow = (x, y) => {
    let result = bigOne;
    let a = x;
    while (true) {
        if ((y & 1) !== 0) result = biMultiply(result, a);
        y >>= 1;
        if (y === 0) break;
        a = biMultiply(a, a);
    }
    return result;
};

const biPowMod = (x, y, m) => {
    let result = bigOne;
    let a = x;
    let k = y;
    while (true) {
        if ((k.digits[0] & 1) !== 0) result = biMultiplyMod(result, a, m);
        k = biShiftRight(k, 1);
        if (k.digits[0] === 0 && biHighIndex(k) === 0) break;
        a = biMultiplyMod(a, a, m);
    }
    return result;
};

class BarrettMu {
    constructor(m) {
        this.modulus = biCopy(m);
        this.k = biHighIndex(this.modulus) + 1;
        const b2k = new BigInt();
        b2k.digits[2 * this.k] = 1;
        this.mu = biDivide(b2k, this.modulus);
        this.bkplus1 = new BigInt();
        this.bkplus1.digits[this.k + 1] = 1;
    }

    modulo(x) {
        const q1 = biDivideByRadixPower(x, this.k - 1);
        const q2 = biMultiply(q1, this.mu);
        const q3 = biDivideByRadixPower(q2, this.k + 1);
        const r1 = biModuloByRadixPower(x, this.k + 1);
        const r2term = biMultiply(q3, this.modulus);
        const r2 = biModuloByRadixPower(r2term, this.k + 1);
        let r = biSubtract(r1, r2);
        if (r.isNeg) r = biAdd(r, this.bkplus1);
        let rgtem = biCompare(r, this.modulus) >= 0;
        while (rgtem) {
            r = biSubtract(r, this.modulus);
            rgtem = biCompare(r, this.modulus) >= 0;
        }
        return r;
    }

    multiplyMod(x, y) {
        const xy = biMultiply(x, y);
        return this.modulo(xy);
    }

    powMod(x, y) {
        let result = new BigInt();
        result.digits[0] = 1;
        let a = x;
        let k = y;
        while (true) {
            if ((k.digits[0] & 1) !== 0) result = this.multiplyMod(result, a);
            k = biShiftRight(k, 1);
            if (k.digits[0] === 0 && biHighIndex(k) === 0) break;
            a = this.multiplyMod(a, a);
        }
        return result;
    }
}

class RSAKeyPair {
    constructor(encryptionExponent, decryptionExponent, modulus) {
        this.e = biFromHex(encryptionExponent);
        this.d = biFromHex(decryptionExponent);
        this.m = biFromHex(modulus);
        this.chunkSize = 2 * biHighIndex(this.m);
        this.radix = 16;
        this.barrett = new BarrettMu(this.m);
    }
}

const getKeyPair = (encryptionExponent, decryptionExponent, modulus) =>
    new RSAKeyPair(encryptionExponent, decryptionExponent, modulus);

const twoDigit = (n) => (n < 10 ? "0" : "") + String(n);

const encryptedString = (key, s) => {
    const a = [];
    const sl = s.length;
    let i = 0;
    while (i < sl) a[i] = s.charCodeAt(i++);

    while (a.length % key.chunkSize !== 0) a[i++] = 0;

    const al = a.length;
    let result = "";

    for (i = 0; i < al; i += key.chunkSize) {
        const block = new BigInt();
        let j = 0;
        for (let k = i; k < i + key.chunkSize; j++) {
            block.digits[j] = a[k++];
            block.digits[j] += a[k++] << 8;
        }
        const crypt = key.barrett.powMod(block, key.e);
        const text = key.radix === 16 ? biToHex(crypt) : biToString(crypt, key.radix);
        result += text + " ";
    }
    return result.substring(0, result.length - 1);
};

const decryptedString = (key, s) => {
    const blocks = s.split(" ");
    let result = "";
    for (let i = 0; i < blocks.length; ++i) {
        const bi = key.radix === 16 ? biFromHex(blocks[i]) : biFromString(blocks[i], key.radix);
        const block = key.barrett.powMod(bi, key.d);
        for (let j = 0; j <= biHighIndex(block); ++j) {
            result += String.fromCharCode(block.digits[j] & 255, block.digits[j] >> 8);
        }
    }
    if (result.charCodeAt(result.length - 1) === 0) {
        result = result.substring(0, result.length - 1);
    }
    return result;
};

setMaxDigits(130);

export default {
    setMaxDigits,
    BigInt,
    biFromNumber,
    biFromDecimal,
    biCopy,
    biToString,
    biToDecimal,
    biFromHex,
    biFromString,
    biAdd,
    biSubtract,
    biMultiply: biMultiply,
    biDivide,
    biModulo,
    biPowMod,
    RSAKeyPair,
    getKeyPair,
    encryptedString,
    decryptedString
};
